package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.entity.Blog;
import com.xxx.entity.MPage;
import com.xxx.entity.query.BlogQuery;
import com.xxx.service.IBlogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-24
 */
@RestController
@RequestMapping("/blog")
@Tag(name = "博客管理")
public class BlogController {
    @Autowired
    private IBlogService blogService;

    @PostMapping("/add")
    @Operation(summary = "添加博客")
    public Result add(@RequestBody Blog blog){
        Boolean flag = blogService.save(blog);
        return flag ? Result.success("添加博客成功") : Result.error("添加博客失败");
    }

    @GetMapping("/delete/{blogId}")
    @Operation(summary = "删除博客")
    public Result delete(@PathVariable Integer blogId){
        Boolean flag = blogService.removeById(blogId);
        return flag ? Result.success("删除博客成功") : Result.error("不存在该博客Id");
    }

    @PutMapping("/update")
    @Operation(summary = "修改博客")
    public Result update(@RequestBody Blog blog){
        Boolean flag = blogService.updateById(blog);
        return flag ? Result.success("修改博客成功") : Result.error("修改博客失败");
    }

    @GetMapping("/selectByPage/{current}/{size}")
    @Operation(summary = "分页查询博客")
    public Result selectByPage(@PathVariable Integer current, @PathVariable Integer size){
        MPage page = blogService.selectByPage(current, size);
        return Result.success(page);
    }

    @PostMapping("/selectByCondition")
    @Operation(summary = "根据博客标题、博客Id、用户Id、用户手机号、用户名条件查询博客")
    public Result selectByCondition(@RequestBody BlogQuery blogQuery){
//        LambdaQueryWrapper<Blog> lqw = new LambdaQueryWrapper<>();
//        lqw.like(blogQuery.getBlogTitle()!=null&&!blogQuery.getBlogTitle().equals(""),
//                        Blog::getBlogTitle,blogQuery.getBlogTitle())
//                .eq(blogQuery.getUserId()!=null,
//                        Blog::getUserId,blogQuery.getUserId())
//                .eq(blogQuery.getBlogId()!=null,
//                        Blog::getBlogId,blogQuery.getBlogId());
//        IPage<Blog> page = blogService.page(new Page<>(blogQuery.getCurrent(), blogQuery.getSize()),
//                lqw);
        MPage page = blogService.selectByCondition(blogQuery);
        return Result.success(page);
    }

    @GetMapping("/selectById/{id}")
    @Operation(summary = "id查询博客")
    public Result selectById(@PathVariable Integer id){
        Blog blog = blogService.getById(id);
        blog.setBlogPlayCount(blog.getBlogPlayCount()+1);
        blogService.updateById(blog);
        return Result.success(blog);
    }
}

